package jastgen;

import java.io.IOException;

import jastgen.frontend.*;
import jastgen.nodes.*;
import jastgen.generator.*;
import jastgen.frontend.ByCaper.*;
import static jastgen.frontend.ByCaper.Token.*;

public class Main {

	public static void main(
			final String[] args
		) throws IOException, CompileException {

		final String outDirPath = "JASTgen_out";

		if(args.length != 1) {
			System.out.println("args[0] must be the input file name.");
			return;
		}

		final String filePath = args[0];

		try {
			final Scanner in = new Scanner(filePath);
			final Parser p = new Parser(new ASTBuilder(in));
			while(true) {
				final Unit t = in.getNextToken();
				p.post(t.t, t.s);
				if(t.t == eof)
					break;
			}

			if(p.isError())
				System.out.println("error.");

			final Module m = (Module)p.accept();
			new Generator(outDirPath).generate(m);

		} catch(final CompileException e) {
			System.out.println(e.getMessage());
		}

		System.out.println("finished.");
	}
}
